// pages/student/application/detail/detail.js
const app = getApp();

Page({
  data: {
    applicationInfo: {
      status: "draft",
      updateTime: "",
      result: "",
      score: 0,
      feedback: "",
      reviewer: "",
    },
    reviewProgress: [],
    loading: true,
  },

  onLoad() {
    this.loadApplicationDetail();
  },

  onShow() {
    this.loadApplicationDetail();
  },

  async loadApplicationDetail() {
    try {
      const local = require("../../../../utils/local");

      // 检查用户登录状态
      const currentUser = local.getCurrentUser();
      if (!currentUser) {
        wx.showToast({
          title: "请先登录",
          icon: "none",
          duration: 2000,
        });
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/login/login",
          });
        }, 2000);
        return;
      }

      const result = local.getApplicationInfo();
      console.log("申请详情结果:", result);

      if (result.success) {
        this.setData({
          applicationInfo: {
            ...result.data.application,
            statusText: this.getStatusText(result.data.application.status),
            statusColor: this.getStatusColor(result.data.application.status),
          },
          reviewProgress: (result.data.progress || []).map((item) => ({
            ...item,
            statusText: this.getProgressStatusText(item.status),
            statusColor: this.getProgressStatusColor(item.status),
          })),
          loading: false,
        });
        console.log("申请状态:", result.data.application.status);
        console.log("评审进度:", result.data.progress);
      } else {
        throw new Error(result.message || "加载失败");
      }
    } catch (error) {
      console.error("加载申请详情失败:", error);
      wx.showToast({
        title: "加载失败",
        icon: "error",
      });
      this.setData({ loading: false });
    }
  },

  // 获取状态显示文本
  getStatusText(status) {
    const statusMap = {
      draft: "草稿",
      submitted: "已提交",
      reviewing: "审核中",
      approved: "已通过",
      rejected: "已驳回",
      completed: "已完成",
    };
    return statusMap[status] || "未知状态";
  },

  // 获取状态颜色
  getStatusColor(status) {
    const colorMap = {
      draft: "#999",
      submitted: "#1890ff",
      reviewing: "#faad14",
      approved: "#52c41a",
      rejected: "#ff4d4f",
      completed: "#52c41a",
    };
    return colorMap[status] || "#999";
  },

  // 获取进度状态文本
  getProgressStatusText(status) {
    const statusMap = {
      pending: "待审核",
      reviewing: "审核中",
      approved: "已通过",
      rejected: "已驳回",
    };
    return statusMap[status] || "待审核";
  },

  // 获取进度状态颜色
  getProgressStatusColor(status) {
    const colorMap = {
      pending: "#999",
      reviewing: "#faad14",
      approved: "#52c41a",
      rejected: "#ff4d4f",
    };
    return colorMap[status] || "#999";
  },
});
